package com.mycompany.controller;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.mycompany.exception.CustomGenericException;
import com.mycompany.service.UserService;
import com.sun.jmx.snmp.Timestamp;


@Controller
public class MainController {
		
	
	private static final Logger logger = LoggerFactory.getLogger(MainController.class);
	
	@Autowired
	private UserService userService;
	
	
	
	// for test service 
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView mainHandler(){
		logger.debug("Home page here ! ");
		
		
		userService.findById(1);
		
		
		
		
		return new ModelAndView("home");
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(
		@RequestParam(value = "error", required = false) String error,
		@RequestParam(value = "logout", required = false) String logout) {
 
		ModelAndView model = new ModelAndView();
		if (error != null) {
			model.addObject("error", "Invalid username and password!");
		}
 
		if (logout != null) {
			model.addObject("msg", "You've been logged out successfully.");
		}
		model.setViewName("login");
		return model;
	}
	
	@RequestMapping(value = "/error", method = RequestMethod.GET)
	public ModelAndView errorHandler(HttpServletRequest req){
		
		CustomGenericException custEx = (CustomGenericException)req.getAttribute("customGenericException");
		logger.debug("Error Page! "+custEx.getErrCode()+" Msg: "+ custEx.getErrMsg());
		return new ModelAndView("error","customGenericException",custEx);
	}
	
	
	@InitBinder
	public void binder(WebDataBinder binder) {binder.registerCustomEditor(Date.class,
	    new PropertyEditorSupport() {
	        public void setAsText(String value) {
	                Date parsedDate;
					try {
						parsedDate = new SimpleDateFormat("yyyy-MM-dd").parse(value);
				
						setValue(new Timestamp(parsedDate.getTime()));
					} catch (ParseException e) {
						 setValue(null);
					}
	        }
	    });
	}
	
}
